package com.ruoyi.safe.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NameUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.safe.domain.CommonCooperator;
import com.ruoyi.safe.domain.CommonCooperatorClass;
import com.ruoyi.safe.domain.CommonCooperatorTeam;
import com.ruoyi.safe.domain.vo.CommonCooperatorClassVO;
import com.ruoyi.safe.mapper.CommonCooperatorClassMapper;
import com.ruoyi.safe.mapper.CommonCooperatorMapper;
import com.ruoyi.safe.mapper.CommonCooperatorTeamMapper;
import com.ruoyi.safe.service.ICommonCooperatorClassService;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 企业班组Service业务层处理
 *
 * @author buxi
 * @date 2023-12-20
 */

@Service
@RequiredArgsConstructor
public class CommonCooperatorClassServiceImpl implements ICommonCooperatorClassService {

    private final CommonCooperatorClassMapper commonCooperatorClassMapper;

    private final CommonCooperatorMapper commonCooperatorMapper;

    private final CommonCooperatorTeamMapper commonCooperatorTeamMapper;

    private final SysUserMapper sysUserMapper;

    /**
     * 查询企业班组
     *
     * @param id 企业班组主键
     * @return 企业班组
     */
    @Override
    public CommonCooperatorClass selectCommonCooperatorClassById(Long id) {
        return commonCooperatorClassMapper.selectCommonCooperatorClassById(id);
    }

    /**
     * 查询企业班组列表
     *
     * @param commonCooperatorClass 企业班组
     * @return 企业班组
     */
    @Override
    public List<CommonCooperatorClassVO> selectCommonCooperatorClassList(CommonCooperatorClass commonCooperatorClass) {

        List<CommonCooperatorClass> commonCooperatorClasses = commonCooperatorClassMapper.selectCommonCooperatorClassList(commonCooperatorClass);
        //查询企业
        List<CommonCooperator> commonCooperators = commonCooperatorMapper.selectSubPackageList();

        //获取班组id
        Long[] teamIds = commonCooperatorClasses.stream()
                .map(CommonCooperatorClass::getTeamId)
                .toArray(Long[]::new);

        //根据班组ids查询出队伍
        List<CommonCooperatorTeam> commonCooperatorTeams = commonCooperatorTeamMapper.selectTeamByIds(teamIds);

        //转换成vo
        List<CommonCooperatorClassVO> commonCooperatorClassVOList = commonCooperatorClasses.stream().map(cooperatorClass -> {

            CommonCooperatorClassVO commonCooperatorClassVO = new CommonCooperatorClassVO();
            BeanUtil.copyProperties(cooperatorClass, commonCooperatorClassVO);
            //设置企业名称
            CommonCooperator commonCooperator = commonCooperators.stream()
                    .filter(s -> s.getId().equals(cooperatorClass.getCooperatorId()))
                    .findFirst()
                    .orElse(new CommonCooperator());

            commonCooperatorClassVO.setCooperatorName(commonCooperator.getEnterpriseName());

            //设置队伍名称
            CommonCooperatorTeam commonCooperatorTeam = commonCooperatorTeams.stream()
                    .filter(s -> s.getId().equals(cooperatorClass.getTeamId()))
                    .findFirst()
                    .orElse(new CommonCooperatorTeam());

            commonCooperatorClassVO.setTeamName(commonCooperatorTeam.getTeamName());

            return commonCooperatorClassVO;
        }).collect(Collectors.toList());

        NameUtils.handleUserNameToNickName(commonCooperatorClassVOList,sysUserMapper.selectUserListAll(),"createBy");

        return commonCooperatorClassVOList;
    }

    /**
     * 新增企业班组
     *
     * @param commonCooperatorClass 企业班组
     * @return 结果
     */
    @Override
    public int insertCommonCooperatorClass(CommonCooperatorClass commonCooperatorClass) {
        commonCooperatorClass.setCreateTime(DateUtils.getNowDate());
        return commonCooperatorClassMapper.insertCommonCooperatorClass(commonCooperatorClass);
    }

    /**
     * 修改企业班组
     *
     * @param commonCooperatorClass 企业班组
     * @return 结果
     */
    @Override
    public int updateCommonCooperatorClass(CommonCooperatorClass commonCooperatorClass) {
        commonCooperatorClass.setUpdateTime(DateUtils.getNowDate());
        return commonCooperatorClassMapper.updateCommonCooperatorClass(commonCooperatorClass);
    }

    /**
     * 批量删除企业班组
     *
     * @param ids 需要删除的企业班组主键
     * @return 结果
     */
    @Override
    public int deleteCommonCooperatorClassByIds(Long[] ids) {
        return commonCooperatorClassMapper.deleteCommonCooperatorClassByIds(ids);
    }

    /**
     * 删除企业班组信息
     *
     * @param id 企业班组主键
     * @return 结果
     */
    @Override
    public int deleteCommonCooperatorClassById(Long id) {
        return commonCooperatorClassMapper.deleteCommonCooperatorClassById(id);
    }

    @Override
    public List<CommonCooperatorClassVO> selectCommonCooperatorClassByProjectId(Long projectId) {
        List<CommonCooperatorClassVO> commonCooperatorClassVOList = new ArrayList<>();
        CommonCooperatorClass commonCooperatorClass = new CommonCooperatorClass();
        commonCooperatorClass.setProjectId(projectId);
        List<CommonCooperatorClass> commonCooperatorClasses = commonCooperatorClassMapper.selectCommonCooperatorClassList(commonCooperatorClass);
        if(commonCooperatorClasses.size() > 0){
            //分包单位名称
            List<CommonCooperator> commonCooperators = commonCooperatorMapper.selectSubPackageList();
            Map<Long,CommonCooperator> commonCooperatorMap = new HashMap<>();
            if(commonCooperators.size() > 0){
                commonCooperatorMap = commonCooperators.stream().collect(Collectors.toMap(CommonCooperator::getId, Function.identity()));
            }
            //队伍
            Long[] teamIds = commonCooperatorClasses.stream().map(CommonCooperatorClass::getTeamId).toArray(Long[]::new);
            List<CommonCooperatorTeam> commonCooperatorTeams = commonCooperatorTeamMapper.selectTeamByIds(teamIds);
            Map<Long,CommonCooperatorTeam> commonCooperatorTeamMap = new HashMap<>();
            if(commonCooperatorTeams.size() > 0){
                commonCooperatorTeamMap = commonCooperatorTeams.stream().collect(Collectors.toMap(CommonCooperatorTeam::getId,Function.identity()));
            }
            for (int i = 0; i < commonCooperatorClasses.size(); i++) {
                CommonCooperatorClass commonCooperatorClass1 = commonCooperatorClasses.get(i);
                CommonCooperatorClassVO commonCooperatorClassVO = new CommonCooperatorClassVO();
                BeanUtils.copyProperties(commonCooperatorClass1,commonCooperatorClassVO);
                CommonCooperator commonCooperator = commonCooperatorMap.get(commonCooperatorClass1.getCooperatorId());
                //设置分包单位名称
                if(commonCooperator != null){
                    commonCooperatorClassVO.setCooperatorName(commonCooperator.getEnterpriseName());
                }
                CommonCooperatorTeam commonCooperatorTeam = commonCooperatorTeamMap.get(commonCooperatorClass1.getTeamId());
                //设置队伍名称
                if(commonCooperatorTeam != null){
                    commonCooperatorClassVO.setTeamName(commonCooperatorTeam.getTeamName());
                }
                commonCooperatorClassVOList.add(commonCooperatorClassVO);
            }
        }
        return commonCooperatorClassVOList;
    }
}
